#!/bin/bash

if [ "$1" = 'redis-cluster' ]; then
    ENDPORT=$((START_PORT+NODE_NUM))
    PORT=${START_PORT}

    if [ "$2" == "start" ]
    then
        STR=""
        while [ $((PORT < ENDPORT)) != "0" ]; do
            echo "Starting $PORT"
            redis-server /usr/local/etc/redis/redis-${PORT}.conf --daemonize yes --port $PORT
#            redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
            STR="${STR} 127.0.0.1:${PORT} "
            PORT=$((PORT+1))
        done
        #exec "redis-cli -a ${REDIS_AUTH} --cluster create --cluster-replicas 1 ${STR}"
        echo "redis-cli -a ${REDIS_AUTH} --cluster create --cluster-replicas 1 ${STR}"
        tail -f /dev/null
    fi

    if [ "$2" == "stop" ]
    then
        while [ $((PORT < ENDPORT)) != "0" ]; do
            echo "Stopping $PORT"
            redis-cli -p $PORT shutdown nosave
            PORT=$((PORT+1))
        done
        exit 0
    fi


else
  exec "$@"
fi